'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Xml
Imports System.Windows.Forms
Imports UA

Public Class amnet
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'extract VIEWSTATE
            strHTMLData = ISPGet("https://myaccount.amnet.com.au/ClientLogin.aspx", CookieContainer, System.Net.CredentialCache.DefaultCredentials)
            Dim VIEWSTATE As String = EncodeHexString(ExtractText(strHTMLData, "__VIEWSTATE", 0, "value=""", """"))

            'login 
            Dim POSTData As String = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" & VIEWSTATE & "&ctl00%24MemberToolsContent%24HiddenField_Redirect=&ctl00%24MemberToolsContent%24txtUsername=" & ConfigurationData.Username & "&ctl00%24MemberToolsContent%24txtPassword=" & ConfigurationData.Password & "&ctl00%24MemberToolsContent%24btnLogin=Login"
            strHTMLData = ISPPost("https://myaccount.amnet.com.au/ClientLogin.aspx", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strHTMLData.Contains("The following error(s) occured") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'usage
            strHTMLData = ISPGet("https://myaccount.amnet.com.au/DSLUsageDetails.aspx", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            'logout
            ISPGet("https://myaccount.amnet.com.au/ClientLogoff.aspx", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'strHTMLData = My.Computer.FileSystem.ReadAllText("C:\programming\isp\amnet\debug.htm")

            Dim PeerPeakDownload As Single
            Dim PeerOffpeakDownload As Single
            Dim PeerPeakUsageLimit As Single
            Dim PeerOffpeakUsageLimit As Single

            If ConfigurationData.OffpeakAvailable = False Then
                ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "Total usage:", 0, "<b>", " "))) * 1000
                PeerPeakDownload = CSng(Val(ExtractText(strHTMLData, "Total usage:", 200, "<b>", " "))) * 1000
                PeerPeakUsageLimit = CSng(Val(ExtractText(strHTMLData, "ctl00_MemberToolsContent_CustomerPlanDetails1_allowances_ctl00_value", 0, ">", "<"))) * 1000
            Else
                ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "border-top:2px solid #000;border-bottom:2px solid #000;""><b", 0, ">", " ", 1))) * 1000
                ISPReturnData.OffpeakDownload = CSng(Val(ExtractText(strHTMLData, "border-top:2px solid #000;border-bottom:2px solid #000;""><b", 0, ">", " ", 2))) * 1000
                PeerPeakDownload = CSng(Val(ExtractText(strHTMLData, "border-top:2px solid #000;border-bottom:2px solid #000;""><b", 0, ">", " ", 3))) * 1000
                PeerOffpeakDownload = CSng(Val(ExtractText(strHTMLData, "border-top:2px solid #000;border-bottom:2px solid #000;""><b", 0, ">", " ", 4))) * 1000
                PeerPeakUsageLimit = CSng(Val(ExtractText(strHTMLData, "ctl00_MemberToolsContent_CustomerPlanDetails1_allowances_ctl00_value", 0, ">", "<"))) * 1000
                PeerOffpeakUsageLimit = CSng(Val(ExtractText(strHTMLData, "ctl00_MemberToolsContent_CustomerPlanDetails1_allowances_ctl03_value", 0, ">", "<"))) * 1000
            End If

            ISPReturnData.LastUpdated = Now

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            If ConfigurationData.OffpeakAvailable = False Then
                If PeerPeakDownload > PeerPeakUsageLimit Then
                    ISPReturnData.ShapedNow = True
                End If
            Else
                If ISPReturnData.OffpeakNow = True Then
                    If PeerPeakDownload > PeerPeakUsageLimit Then
                        ISPReturnData.ShapedNow = True
                    End If
                Else
                    If PeerOffpeakDownload > PeerOffpeakUsageLimit Then
                        ISPReturnData.ShapedNow = True
                    End If
                End If
            End If

            '##########################################################

            'create graph data

            If ConfigurationData.OffpeakAvailable = False Then
                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))

                'peering peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak Peering " & Format(System.Math.Floor((PeerPeakDownload / PeerPeakUsageLimit) * 1000) / 10, "#0.0") & "% (" & PeerPeakDownload & " MB)", PeerPeakDownload, PeerPeakUsageLimit, False))
            Else
                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'peering peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak Peering " & Format(System.Math.Floor((PeerPeakDownload / PeerPeakUsageLimit) * 1000) / 10, "#0.0") & "% (" & PeerPeakDownload & " MB)", PeerPeakDownload, PeerPeakUsageLimit, ISPReturnData.PeakNow))

                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((ISPReturnData.OffpeakDownload / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))

                'peering offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak Peering " & Format(System.Math.Floor((PeerOffpeakDownload / PeerOffpeakUsageLimit) * 1000) / 10, "#0.0") & "% (" & PeerOffpeakDownload & " MB)", PeerOffpeakDownload, PeerOffpeakUsageLimit, ISPReturnData.OffpeakNow))
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data

            'peak
            ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
            ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

            If ConfigurationData.OffpeakAvailable = True Then
                'offpeak
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak D/L Left :", ISPReturnData.OffpeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Avg. Left :", ISPReturnData.OffpeakUsageRemainingPerDayDescription))
            End If

            'peering peak
            ISPReturnData.TextItemList.Add(New TextItem("Pk Peer Left :", PeerPeakUsageLimit - PeerPeakDownload & " MB"))
            If ISPReturnData.DaysLeft > 0 Then
                ISPReturnData.TextItemList.Add(New TextItem("Pk Peer Av Left :", CInt((PeerPeakUsageLimit - PeerOffpeakDownload) / ISPReturnData.DaysLeft) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Pk Peer Av Left :", CInt(PeerPeakUsageLimit - PeerOffpeakDownload) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)"))
            End If

            'peering offpeak
            If ConfigurationData.OffpeakAvailable = True Then
                ISPReturnData.TextItemList.Add(New TextItem("OfPk Peer Left :", PeerOffpeakUsageLimit - PeerOffpeakDownload & " MB"))
                If ISPReturnData.DaysLeft > 0 Then
                    ISPReturnData.TextItemList.Add(New TextItem("OfPk Peer Av Left :", CInt((PeerOffpeakUsageLimit - PeerOffpeakDownload) / ISPReturnData.DaysLeft) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("OfPk Peer Av Left :", CInt(PeerOffpeakUsageLimit - PeerOffpeakDownload) & " MB/Day (" & ISPReturnData.DaysLeft & " Days)"))
                End If
            End If

            If ISPReturnData.ShapedNow = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class

